from django.conf import settings

from EE.models import StateMill
from EE.models import Windmill

def createNew(reference_state, reference_arr):
  arr = []
  for (number, mill) in reference_arr:
    state_mill = StateMill(state=reference_state, typeOfMill=mill)
    state_mill.noOfMaintenance = 0
    state_mill.noOfBrokenMills = 0
    state_mill.noOfMills = 0
    state_mill.noOfMills1Month = 0
    state_mill.noOfMills2Months = 0
    state_mill.noOfMills3Months = number
    state_mill.save()
    arr.append(state_mill)
  return arr
  
def handle_made_turn(reference_state, mill_arr):
  #Nu weer wel
  for (mill, number, maintenance) in mill_arr:
    windmill = Windmill.objects.filter(name=mill).get()
    state_mill = StateMill.objects.filter(state=reference_state).filter(typeOfMill=windmill).get()
    state_mill.noOfMills = number
    state_mill.noOfMaintenance = maintenance
    state_mill.save()
	
def update(reference_mill, new_game_state):
  state_mill = StateMill(state=new_game_state, typeOfMill=reference_mill.typeOfMill)
  state_mill.noOfMaintenance = reference_mill.noOfMaintenance
  state_mill.noOfBrokenMills = reference_mill.noOfBrokenMills
  state_mill.noOfMills = reference_mill.noOfMills
  state_mill.noOfMills1Month = reference_mill.noOfMills1Month
  state_mill.noOfMills2Months = reference_mill.noOfMills2Months
  state_mill.noOfMills3Months = reference_mill.noOfMills3Months
  state_mill.save()
  return state_mill
  
def getStateMills(reference_state):
  if StateMill.objects.filter(state=reference_state).count() == 0:
    raise StateMill.DoesNotExist
  else:
    state_mills = StateMill.objects.filter(state=reference_state)
    return state_mills